<template>
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            label-width="68px"
        >
            <el-form-item label="员工姓名" prop="realName">
                <el-input
                    v-model="queryParams.realName"
                    placeholder=""
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="工号" prop="staffNum">
                <el-input
                    v-model="queryParams.staffNum"
                    placeholder=""
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="员工状态" prop="political">
                <el-select
                    v-model="queryParams.staffType"
                    placeholder=""
                    clearable
                >
                    <el-option
                        v-for="dict in dict.type.hrm_staff_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="部门" prop="orgId">
                <treeselect
                    v-model="queryParams.orgId"
                    :options="deptOptions"
                    :show-count="true"
                    placeholder=""
                    @keyup.enter.native="handleQuery"
                    style="width: 215px"
                />
            </el-form-item>
            <el-form-item label="岗位类别" prop="postTypeCode">
                <el-select
                    v-model="queryParams.postTypeCode"
                    placeholder=""
                    @change="getPostList2"
                >
                    <el-option
                        v-for="dict in dict.type.job_category"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="岗位名称" prop="postId">
                <el-select v-model="queryParams.postId" placeholder="">
                    <el-option
                        v-for="item in postList2"
                        :key="item.postId"
                        :label="item.postName"
                        :value="item.postId"
                    ></el-option>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleQuery"
                    >查询</el-button
                >
                <el-button
                    icon="el-icon-refresh"
                    size="mini"
                    @click="resetQuery"
                    >重置</el-button
                >
            </el-form-item>
        </el-form>
        <TableCard>
            <el-table
                :border="Global.tableShowBorder"
                v-loading="loading"
                :data="inductionList"
                @selection-change="handleSelectionChange"
            >
                <el-table-column label="序号" type="index" align="center" />
                <el-table-column
                    label="姓名(工号)"
                    align="center"
                    prop="staffNum"
                    width="200"
                >
                    <template slot-scope="scope">
                        <span>{{
                            scope.row.realName + "(" + scope.row.staffNum + ")"
                        }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="性别" align="center" prop="sex">
                    <template slot-scope="scope">
                        <dict-tag
                            :options="dict.type.sys_user_sex"
                            :value="scope.row.sex"
                        />
                    </template>
                </el-table-column>
                <el-table-column
                    label="员工状态"
                    align="center"
                    prop="staffType"
                >
                    <template slot-scope="scope">
                        <dict-tag
                            :options="dict.type.hrm_staff_type"
                            :value="scope.row.staffType"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="部门" align="center" prop="orgId">
                    <template slot-scope="scope">
                        <span>{{ parseSelectTree(scope.row.orgId) }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="岗位类别"
                    align="center"
                    prop="postTypeCode"
                >
                    <template slot-scope="scope">
                        <span>{{
                            parsePostTypeCode(scope.row.postTypeCode)
                        }}</span>
                    </template>
                </el-table-column>

                <el-table-column label="岗位名称" align="center" prop="postId">
                    <template slot-scope="scope">
                        <span>{{
                            parsePostName(scope.row.postId) || "—"
                        }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="入职日期"
                    align="center"
                    prop="entryTime"
                    width="180"
                >
                    <template slot-scope="scope">
                        <span>{{
                            parseTime(scope.row.entryTime, "{y}-{m}-{d}") || "—"
                        }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="退休日期"
                    align="center"
                    prop="exTxDate"
                    width="180"
                >
                    <template slot-scope="scope">
                        <span>{{
                            parseTime(scope.row.exTxDate, "{y}-{m}-{d}") || "—"
                        }}</span>
                    </template>
                </el-table-column>

                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                    width="180"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            v-if="scope.row.staffType == '5'"
                            @click="handleUpdateEdit(scope.row)"
                            v-hasPermi="['staff:induction:edit']"
                            >编辑</el-button
                        >
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-view"
                            @click="handleView(scope.row)"
                            v-hasPermi="['staff:induction:edit']"
                            >查看</el-button
                        >
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-plus"
                            @click="handleUpdate(scope.row)"
                            v-if="
                                (scope.row.staffType != '2' &&
                                    scope.row.staffType != '3') ||
                                ($store.getters.allDefectObj['HR-WEB-056'] ==
                                    0 &&
                                    scope.row.staffType == '2')
                            "
                            v-hasPermi="['staff:induction:edit']"
                            >退休</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>

            <pagination
                v-show="total > 10"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
            />
        </TableCard>
    </div>
</template>

<script>
import {
    listInduction,
    getInduction,
    delInduction,
    addInduction,
    updateInduction,
    createUser,
} from "@/api/hrm/staff/induction";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listPostAll, listPost } from "@/api/system/post";
import { mapGetters, mapMutations } from "vuex";
import { ReactiveFlags } from "vue";

const defaultQueryParams = {
    postId:null , 
    pageNum: 1,
    pageSize: 10,

    realName: null,
    sex: null,
    nation: null,
    birthdate: null,
    political: null,
    joinpartyDate: null,
    marryStatus: null,
    height: null,
    weight: null,
    nativePlace: null,
    idNum: null,
    phone: null,
    mail: null,
    driverslicenseType: null,
    englishLevel: null,
};

export default {
    name: "retire",
    dicts: [
        "political_status",
        "education_list",
        "nation_list",
        "marry_status",
        "language_level",
        "sys_user_sex",
        "driverslicense_type",
        "hrm_staff_type",
        "hrm_staff_type",
        "job_category",
    ],
    components: { Treeselect },
    data() {
        return {
            // 部门树选项
            deptOptions: [],
            // 职位选项
            postList: [],
            // 遮罩层
            loading: true,
            // 选中数组

            showSearch: true,
            // 总条数
            total: 0,
            // 员工入职表格数据
            inductionList: [],
            // 员工入职信息登记--教育经历表格数据
            staffInductionEduexpList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                ...defaultQueryParams,
                params: {
                    order:
                        this.$store.getters.allDefectObj["HR-WEB-053"] == 0
                            ? ""
                            : "4",
                },
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            //存储拍平后的selectTree数据
            deptOptionsArray: null,
            postList2: [], //给查询用
        };
    },

    computed: {
        ...mapGetters("retire", ["searchCache"]),

        matchPostId() {
            return this.postList.find((item) => item.id === this.postId);
        },
    },
    created() {
        if (this.searchCache) {
            this.queryParams = { ...this.searchCache };
        }
        this.getList();
        //查询基础数据
        this.getTreeselect();
        this.getPostList();
    },
    methods: {
        ...mapMutations("retire", ["setSearchCache"]),

        parsePostTypeCode(key) {
            if (key) {
                let tempObj = this.dict.type.job_category.find(
                    (item) => item.value == key
                );
                if (tempObj) {
                    return tempObj.label;
                }
            }

            return "—";
        },
        getPostList2(key) {
            this.queryParams.postId = undefined;
            listPost({
                pageSize: 100,
                postTypeCode: key || "",
            }).then((res) => {
                this.postList2 = res.rows;
            });
        },
        parseSelectTree(id) {
            let tempObj = this.deptOptionsArray.find((item) => item.id == id);
            if (tempObj) {
                return tempObj.label;
            }
            return "—";
        },
        flattenArray(arr) {
            let result = [];
            for (let i = 0; i < arr.length; i++) {
                let item = arr[i];
                result.push(item);
                if (item.children) {
                    result = result.concat(this.flattenArray(item.children));
                }
            }
            return result;
        },

        parsePostName(ids) {
            let nameArr = [];
            let selIds = ids.split(",");
            this.postList.forEach((item) => {
                selIds.forEach((sel) => {
                    if (item.postId == sel) {
                        nameArr.push(item.postName);
                    }
                });
            });
            return nameArr.join(",");
        },
        getPostList() {
            listPostAll().then((res) => {
                this.postList = res.data;
            });
        },
        /** 查询部门下拉树结构 */
        getTreeselect() {
            treeselect().then((response) => {
                this.deptOptions = response.data;
                console.log(this.deptOptions, "this.deptOptions");

                this.deptOptionsArray = this.flattenArray(this.deptOptions);
                console.log(this.deptOptionsArray, " this.deptOptionsArray ");
            });
        },
        /** 查询员工入职列表 */
        getList() {
            this.loading = true;
            listInduction(this.queryParams).then((response) => {
                this.inductionList = response.rows;
                this.total = response.total;
                this.loading = false;
                console.log(this.inductionList, "this.inductionList");
            });
        },
        /** 查询按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.setSearchCache(this.queryParams);
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.setSearchCache(null);
            // this.resetForm("queryForm");
            this.queryParams = { ...defaultQueryParams };
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.$tab.openPage(
                "员工入职登记信息表",
                "/hrm/staff/induction/form/add"
            );
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            const id = row.id || this.ids;
            // this.$tab.openPage("员工入职登记信息表", "/hrm/staff/book/form/"+id);
            this.$router.push({
                path: "/hrm/staff/retire/form/" + row.id,
                query: { readonly: "0" },
            });
        },
        handleUpdateEdit(row) {
            const id = row.id || this.ids;
            // this.$tab.openPage("员工入职登记信息表", "/hrm/staff/book/form/"+id);
            this.$router.push({
                path: "/hrm/staff/retire/form/" + row.id,
                query: { readonly: "0", flag: "yes" },
            });
        },

        /** 员工创建系统用户 **/
        createUser(row) {
            this.$modal.confirm("确认创建系统用户？").then(() => {
                return createUser(row.id).then((res) => {
                    if (200 == res.code) {
                        this.$modal.msgSuccess("创建用户成功");
                    }
                });
            });
        },
        /** 查看单据详情 */
        handleView(row) {
            this.$router.push({
                path: "/hrm/staff/retire/form/" + row.id,
                query: { readonly: "1" },
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm('是否确认删除员工入职编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delInduction(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {});
        },
        /** 员工入职信息登记--教育经历序号 */
        rowPlStaffInductionEduexpIndex({ row, rowIndex }) {
            row.index = rowIndex + 1;
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "hrm/staff/induction/export",
                {
                    ...this.queryParams,
                },
                `induction_${new Date().getTime()}.xlsx`
            );
        },

        /** 提交审批 ***/
        doSubmitFlow(row) {
            let data = {
                businessId: row.id,
                billType: BillType.billType.STAFF_INDUCTION,
            };
            this.$modal
                .confirm("确认要提交该单据进行审批吗？")
                .then(() => {
                    return submitFlow(data);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("提交成功");
                })
                .catch(() => {});
        },
        /** 取回审批 ***/
        doCancleFlow(row) {
            let data = {
                businessId: row.id,
                billType: BillType.billType.STAFF_INDUCTION,
            };
            this.$modal.confirm("确认要取回该协议审批吗？").then(() => {
                cancleFlow(data).then((res) => {
                    if (200 == res.code) {
                        this.getList();
                        this.$modal.msgSuccess("取回成功");
                    }
                });
            });
        },
        /** 流程信息 ***/
        flowDetail(row) {
            this.businessId = row.id + "";
            this.showFlowInfo = true;
        },
        //关闭流程信息窗口
        closeFlowInfoDialog() {
            this.showFlowInfo = false;
        },
    },
};
</script>
